package com.sands.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.sands.R;
import com.sands.ui.adapter.Viewpager_Adapter;
import com.sands.ui.fragment.FragmentDemo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends AppCompatActivity {

    private AdView mAdView;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
        MobileAds.initialize(this, "ca-app-pub-7338833287141301~6711935909");
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
                Log.i("Ads", "onAdLoaded");
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.
                Log.i("Ads", "onAdFailedToLoad");

            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
                Log.i("Ads", "onAdOpened");
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
                Log.i("Ads", "onAdLeftApplication");
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when when the user is about to return
                // to the app after tapping on an ad.
                Log.i("Ads", "onAdClosed");
            }
        });

        initTabLayout();
    }


    private void initTabLayout() {
        FragmentManager fm = getSupportFragmentManager();
        //和viewpager相关联
       List<Fragment> list = new ArrayList<>();
        //听课记录
        FragmentDemo demo1 = FragmentDemo.newInstance(1, true);
        //听课交流
        FragmentDemo demo2 = FragmentDemo.newInstance(2, true);
        FragmentDemo demo3 = FragmentDemo.newInstance(3, true);
        list.add(demo1);
        list.add(demo2);
        list.add(demo3);

        List<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");

        Viewpager_Adapter adapter = new Viewpager_Adapter(fm, list, strings);
        mViewPager.setAdapter(adapter);
        //mViewPager.setOffscreenPageLimit(2);
    }
}
